package com.example.zhaojianxu.signin.psychological.test

import android.graphics.Color
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.util.Log
import android.view.View
import com.example.zhaojianxu.signin.R
import com.example.zhaojianxu.signin.bean.BaseActivity
import com.example.zhaojianxu.signin.common.constance.ComConstance
import com.example.zhaojianxu.signin.common.http.HttpUtil
import com.example.zhaojianxu.signin.psychological.http.PsycEntity
import com.example.zhaojianxu.signin.psychological.result.PResultActivity
import kotlinx.android.synthetic.main.activity_test.*
import kotlinx.android.synthetic.main.common_title_bar.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class TestActivity : BaseActivity(),IPTestListener {

    var mAdapter: TestAdapter? = null
    var mList: MutableList<Fragment> = mutableListOf()
    var mResult = 0

    override fun initData() {

//        for (it in 0..4){
//            val f = TestFragment()
//            val bundle = Bundle()
//            bundle.putInt("position",it)
//            bundle.putSerializable("model",PsycEntity())
//            f.arguments = bundle
//
//            mList.add(f)
//        }

        HttpUtil.instance.getPsycApi()?.postQueryTest()?.enqueue(object: Callback<MutableList<PsycEntity>>{
            override fun onFailure(call: Call<MutableList<PsycEntity>>, t: Throwable) {

            }

            override fun onResponse(call: Call<MutableList<PsycEntity>>, response: Response<MutableList<PsycEntity>>) {
                if (response.isSuccessful && response.body()?.isNotEmpty() == true){
                    for (position in 0 until response.body()?.size!!)
                    {
                        val f = TestFragment()
                        val bundle = Bundle()
                        bundle.putInt("position",position)
                        bundle.putSerializable("model",response.body()?.get(position))
                        f.arguments = bundle

                        mList.add(f)
                    }
                }
                if (mList.size == 1){
                    overTest()
                }
                mAdapter?.notifyDataSetChanged()

            }
        })

    }

    override fun initView() {
        mAdapter = TestAdapter(supportFragmentManager,mList)
        activity_test_vp.adapter = mAdapter

        common_setting.visibility = View.GONE
        common_title.text = "心理测试"
        common_back.setOnClickListener {
            finish()
        }

        activity_test_vp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(p0: Int) {
            }

            override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
            }

            override fun onPageSelected(p0: Int) {
                if (p0 == mList.size-1){
                    overTest()
                }else{
                    common_setting.visibility = View.GONE
                }
                Log.d("TAGGGGG","onPageSelected   = ${p0}")
            }

        })
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)
    }

    override fun success(position: Int, result: Int) {
        mResult += result
        if (position < mList.size - 1){
            Log.d("TAGGGGG","result = ${mResult}")
        }else {
            Log.d("TAGGGGG","really result = ${mResult}")
        }
    }

    fun overTest(){
        common_setting.visibility = View.VISIBLE
        common_setting.text = "完成"
        common_setting.setTextColor(Color.WHITE)
        common_setting.setOnClickListener {
            upCode()

        }
    }

    fun upCode(){
        HttpUtil.instance.getPsycApi()?.postUpcode(ComConstance.UID,mResult)?.enqueue(object: Callback<String>{
            override fun onFailure(call: Call<String>, t: Throwable) {

            }

            override fun onResponse(call: Call<String>, response: Response<String>) {
                if (response.isSuccessful && response.body()?.isNotEmpty() == true && response.body()?.equals("true") == true){
                    toOtherActivity(PResultActivity::class.java)
                    finish()
                }
            }

        })
    }
}
